﻿#region Imports
using System;
using System.Drawing;
using System.Windows.Forms;
using CommonUtilities;
#endregion

namespace BulkRenamer.Util.Controls
{
    /// <summary>
    /// BulkRenamer TextBox
    /// </summary>
    public class BRTextBox : TextBox
    {
        #region Properties
        private FontStyle initFontStyle;
        /// <summary>
        /// Stores font-style used at initialization
        /// </summary>
        public FontStyle InitialFontStyle { get { return initFontStyle; } }

        private Cursor initCursor;
        /// <summary>
        /// Stores cursor used at initialization
        /// </summary>
        public Cursor InitialCursor { get { return initCursor; } }

        /// <summary>
        /// Watermark text
        /// </summary>
        public string WatermarkText { get; set; }

        /// <summary>
        /// Focuses automatically on mouse hover, if TRUE
        /// </summary>
        public bool FocusOnHover { get; set; }

        public bool LinkBehaviour { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public BRTextBox()
            : base()
        {
            // Default values of properties
            FocusOnHover = true;
            LinkBehaviour = true;
            Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));

            // Event assignment
            this.MouseEnter += new EventHandler(BRTextBox_MouseEnter);
            this.MouseLeave += new EventHandler(BRTextBox_MouseLeave);
        }
        #endregion

        #region Formatting events
        /// <summary>
        /// MouseEnter event of BRTextBox
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">EventArgs</param>
        private void BRTextBox_MouseEnter(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (FocusOnHover) { txt.Focus(); /* SendKeys.Send("{End}");*/ }
            txt.ForeColor = Color.Blue;

            // Back-up of initialized values
            this.initFontStyle = txt.Font.Style;
            this.initCursor = txt.Cursor;

            if (LinkBehaviour && Form.ModifierKeys == Keys.Control)
            {
                txt.Font = new Font(txt.Font.FontFamily, txt.Font.Size, FontStyle.Underline, txt.Font.Unit, txt.Font.GdiCharSet);
                txt.Cursor = Cursors.Hand;
            }
        }

        /// <summary>
        /// MouseLeave event of BRTextBox
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">EventArgs</param>
        private void BRTextBox_MouseLeave(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            txt.ForeColor = Color.Black;

            if (LinkBehaviour)
            {
                txt.Font = new Font(txt.Font.FontFamily, txt.Font.Size, InitialFontStyle, txt.Font.Unit, txt.Font.GdiCharSet);
                txt.Cursor = InitialCursor;
            }
        }
        #endregion

        #region Methods
        public override string ToString()
        {
            return Utilities.ToString(this);
        }
        #endregion
    }
}
